package com.example.anni.zhuzhoujia.tianya12345;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by yangshengwen2016 on 2016/6/11.
 * 活动报名窗口
 */
public class CityParticipate extends AppCompatActivity {
    private String id;
    private EditText phone;
    private EditText content;
    private String phoneValue = "";
    private String contentValue = "";
    private Button bm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_participate);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("参与活动");//设置标题
        LinearLayout city_return = (LinearLayout) findViewById(R.id.city_return);//返回图片
        city_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //调用登录接口
        RequestParams params1 = new RequestParams("http://tr.zzapi.gson.cn/api/user/login");
        params1.addBodyParameter("userName", "测试");
        params1.addBodyParameter("password", "666666");
        x.http().post(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
        //获取外面传过来的id
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        phone = (EditText) findViewById(R.id.phone);
        content = (EditText) findViewById(R.id.content);
        //设置编辑框焦点情况
        phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phone.setBackgroundResource(R.drawable.edit_background1);
                } else {
                    phone.setBackgroundResource(R.drawable.edit_background2);
                }
            }
        });
        content.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    content.setBackgroundResource(R.drawable.edit_background1);
                } else {
                    content.setBackgroundResource(R.drawable.edit_background2);
                }
            }
        });
        bm = (Button) findViewById(R.id.bm);
        //报名按钮监听器
        bm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phoneValue = phone.getText().toString();
                contentValue = content.getText().toString();
                if (phoneValue.length() == 11 & contentValue.length() != 0) {
                    //参与活动
                    RequestParams params = new RequestParams("http://tr.zzapi.gson.cn/api/activity/partIn");
                    params.addBodyParameter("cid", id);
                    params.addBodyParameter("content", contentValue);
                    params.addBodyParameter("telephone", phoneValue);
                    x.http().post(params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            JSONObject json1 = JSON.parseObject(s);
                            String success = json1.getString("success");
                            if (success.equals("true")) {
                                Toast.makeText(CityParticipate.this, "参与成功！", Toast.LENGTH_LONG).show();
                                finish();
                            } else {
                                Toast.makeText(CityParticipate.this, "参与失败！", Toast.LENGTH_LONG).show();
                            }

                        }

                        @Override
                        public void onError(Throwable throwable, boolean b) {
                            Toast.makeText(CityParticipate.this, "出错！  出错原因：" + throwable, Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onCancelled(CancelledException e) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });
                    //发表成功则关闭当前activity
                    finish();
                } else if (phoneValue.length() == 0 | contentValue.length() == 0) {
                    Toast.makeText(CityParticipate.this, "请输入联系方式和内容!", Toast.LENGTH_LONG).show();
                } else if (phoneValue.length() > 0 & phoneValue.length() != 11 & contentValue.length() > 0) {
                    Toast.makeText(CityParticipate.this, "请输入11位电话号码！", Toast.LENGTH_LONG).show();
                } else {

                }
            }
        });
    }
}
